//
//  ByViewSubClassificationScroll.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewSubClassificationScroll.h"

static CGFloat kLayoutMargin = 5;

@implementation ByCellSubClassificationItem
{
    UIImageView *_mainView;
    UILabel     *_nameLabel;
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _mainView = [[UIImageView alloc] init];
    [self addSubview:_mainView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = FONT(13);
    _nameLabel.text = @"";
    _nameLabel.textColor = [UIColor grayColor];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_nameLabel];
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _mainView.frame = CGRectMake(kLayoutMargin, kLayoutMargin, self.width - kLayoutMargin * 2, self.width - kLayoutMargin * 2);
    _nameLabel.frame = CGRectMake(0, _mainView.bottom, self.width, self.height - self.width);
    
    int r = arc4random_uniform(255);
    int g = arc4random_uniform(255);
    int b = arc4random_uniform(255);
    _mainView.backgroundColor = rgba(r, g, b, 1);
    _nameLabel.text = _level3Model.name;
    
}
- (void)setLevel3Model:(ByModelSubClassification *)level3Model {
    
    _level3Model = level3Model;
    
    [self setNeedsLayout];
}

@end


#pragma mark - 主视图
@implementation ByViewSubClassificationScroll

- (instancetype)initWithFrame:(CGRect)frame {
    
    // 设置布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsZero;
    CGFloat width = (frame.size.width) / 3;
    CGFloat height = width + 25;
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize = CGSizeMake(frame.size.width, 40);

    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {

        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self setupDefault];
    }
    
    return self;
}

- (void)setupDefault {

    self.showsVerticalScrollIndicator = NO;
    self.delegate = self;
    self.dataSource = self;
    [self registerClass:[ByCellSubClassificationItem class] forCellWithReuseIdentifier:@"cell"];
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}


#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.level2Models.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    ByModelSubClassification *level2Model = self.level2Models[section];
    return level2Model.subClasses.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellSubClassificationItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    ByModelSubClassification *level2Model = self.level2Models[indexPath.section];
    
    cell.level3Model = level2Model.subClasses[indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        [header.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UILabel *classLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
        classLabel.adjustsFontSizeToFitWidth = YES;
        classLabel.font = FONT(14);
        classLabel.textColor = [UIColor blackColor];
        classLabel.textAlignment = NSTextAlignmentLeft;
        [header addSubview:classLabel];
        
        ByModelSubClassification *level2Model = self.level2Models[indexPath.section];
        classLabel.text = level2Model.name;
        
        return header;
    }
    
    return nil;
}

@end
